package com.aftersale.demo.service.impl;

import com.aftersale.demo.Utils.PswUtil;
import com.aftersale.demo.Utils.Result;
import com.aftersale.demo.entity.User;
import com.aftersale.demo.mapper.UserMapper;
import com.aftersale.demo.service.UserService;
import com.alibaba.druid.util.Utils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    UserMapper userMapper;
    @Override
    public Result login(User user) {

        if(user.getUsername().isEmpty()||user.getPassword().isEmpty()){
            return Result.error("用户名或密码不能为空");
        }
        else{
            User dbuser =userMapper.login(user);
            if(dbuser==null){
                return Result.error("用户名错误");
            }else if(!dbuser.getPassword().equals(Utils.md5(user.getPassword()+dbuser.getMd5()))){
                return Result.error("密码错误");
            }else{


                return Result.success("登录成功，欢迎回来",user);
            }

        }
    }



    @Override
    public Result register(User user) {

        if(user.getUsername().isEmpty()||user.getPassword().isEmpty()){
            return Result.error("用户名或密码不能为空");
        }else if(userMapper.exits(user)!=null){
            return Result.error("用户名已存在");
        }else{
            user.setMd5(PswUtil.salt());
            user.setPassword(Utils.md5(user.getPassword()+ user.getMd5()));
            userMapper.register(user);
            return Result.success();
        }
    }
}




